package com.Web.Servlet;

import com.Web.Person.Course;
import com.Web.Person.Query.StudentQuery;
import com.Web.Person.Student;
import com.Web.Person.VO.StudentVO;
import com.Web.Service.ICourseService;
import com.Web.Service.IStudentService;
import com.Web.Service.Impl.StudentServiceImpl;
import com.Web.util.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    private IStudentService studentService = new StudentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决post乱码问题
        //req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectAll":
                selectAll(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
        }
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("StudentServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        System.out.println(Arrays.toString(ids));
        studentService.deleteAll(ids);
        Result result = new Result(0,"删除成功");
        JSONUtil.toJSON(resp,result);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String banjiIds = req.getParameter("banjiId");
        Integer banjiId;
        if (banjiIds == null) {
            banjiId = null;
        } else {
            banjiId = Integer.parseInt(banjiIds);
        }
        Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(age), gender,banjiId);
        studentService.Update(student);
        Result result = new Result(0,"修改成功");
        JSONUtil.toJSON(resp,result);
        //resp.sendRedirect("/student");
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.selectById");
        String id = req.getParameter("id");
        StudentVO studentVO = studentService.selectById(Integer.parseInt(id));
        //Result result = new Result(0,"修改成功", course);
        JSONUtil.toJSON(resp, Result.ok(studentVO));
//        req.setAttribute("student",student);
//        req.getRequestDispatcher("/student_update.jsp").forward(req,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.add");
        String name = req.getParameter("name");
        String ages = req.getParameter("age");
        int age = Integer.parseInt(ages);
        String gender = req.getParameter("gender");
        String banjiIds = req.getParameter("banjiId");
        Integer banjiId;
        if (banjiIds == null) {
            banjiId = null;
        } else {
            banjiId = Integer.parseInt(banjiIds);
        }
        Student student = new Student(name, age, gender,banjiId);
        System.out.println(student);
        studentService.add(student);
        Result result = new Result(0,"添加成功");
        JSONUtil.toJSON(resp,result);
        //resp.sendRedirect("/student_list.jsp");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.deleteById");
        String id = req.getParameter("id");
        System.out.println("id:" + id);
        studentService.deleteById(Integer.parseInt(id));
        Result result = new Result(0,"删除成功");
        JSONUtil.toJSON(resp,result);
//        resp.setContentType("text/html;charset=UTF-8");
//        ObjectMapper mapper = new ObjectMapper();
//        mapper.writeValue(resp.getWriter(), result);
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.selectAll");
        List<Student> list = studentService.selectAll();
        req.setAttribute("list", list);
        req.getRequestDispatcher("student_list.jsp").forward(req, resp);
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.selectByPage");
        String page = req.getParameter("page");
        if(page == null || page.equals("")) {
            page = "1";
        }
        String limit = req.getParameter("limit");
        if(limit == null || limit.equals("")) {
            limit = "5";
        }
        System.out.println(page + limit);
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String banji = req.getParameter("banji");
        System.out.println(name + age + gender + banji);
        StudentQuery studentQuery = new StudentQuery(Integer.parseInt(page),Integer.parseInt(limit),name,age,gender,banji);
        PageResult pageResult = studentService.selectByPage(studentQuery);
        System.out.println(pageResult);
        JSONUtil.toJSON(resp,pageResult);
//        resp.setContentType("text/html;charset=UTF-8");
//        ObjectMapper mapper = new ObjectMapper();
//        mapper.writeValue(resp.getWriter(),pageResult);
    }
}
